<?php   // $Id: bon_lister.php 288 2008-09-20 09:35:54Z pcoustillas $ ?>
<?php
/*
Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx
Modification : Pierre Coustillas
*/
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");
require_once("include/headers.php");
?>

<script type="text/javascript" src="javascripts/confdel.js"></script>
<script type="text/javascript" src="javascripts/tri_formulaire.js"></script>
<script language="javascript" src="javascripts/verifier_form.js" type="text/javascript"></script>
<script type="text/javascript">
	function verif_creer_bon()
	{
		if (verifier('formu','black','date','vide#date_','num_client','egale_a%0'))
		{
			document.formu.submit();
		} else {
			alert("<?php aff_lang('Une des valeurs saisie n\'est pas correct.\nMerci de corriger avant de poursuivre.') ?>")	
		}

	}
</script>

<?php
require_once("include/combo_annee.php");
?>



<table class="page" align="center">
	<tr>
		<td class="page" align="center">
			<?php
			require_once("include/head.php");
			?>
		</td>
	</tr>
	<tr>
		<td class="page" align="center">

			<?php
			if ($user_com == "n") {
				echo"<h1>$lang_commande_droit</h1>";
				exit;
			}

			if (isset($_GET['message']))
			{
				echo "<h1>" . stripslashes($_GET['message']) . "</h1>";
			}

			$jour = date("d");
			$mois = date("n"); //ne doit pas utiliser m car si zero devant le mois marche pas ou mal pour lire le tableau $calendrier
			$annee = date("Y");
			//pour le formulaire
			$mois_1=isset($_POST['mois_1'])?$_POST['mois_1']:$mois;
			$annee_1=isset($_POST['annee_1'])?$_POST['annee_1']:$annee;
			//$tri_ordre=isset($_POST['tri_ordre'])?$_POST['tri_ordre']:"ASC"; //ordre de tri
			//$tri_champ=isset($_POST['tri_champ'])?$_POST['tri_champ']:"date"; // champ de tri

			
			$sql = "SELECT mail, login, num_client, num_bon, tot_htva, tot_tva, nom, num_facture,
			date,(tot_htva + tot_tva) as ttc
					FROM ${tblpref}bon_comm
					RIGHT JOIN ${tblpref}client on ${tblpref}bon_comm.client_num = num_client ";


			// ----------  DEBUT DE GESTION DES FILTRES ---------------------
			if (isset($_GET['annuler_filtre'])) //vidage des filtres si demandé
			{
				$_SESSION['filtre_bon_client'] = "";
				$_SESSION['filtre_bon_num'] = "";
				$_SESSION['filtre_bon_date'] = "";
				$_SESSION['filtre_bon_HT'] = "";
				$_SESSION['filtre_bon_TTC'] = "";
				$_SESSION['filtre_a_facturer'] = "1"; // 1 ou ""
				$_SESSION['filtre_deja_facture'] = "";
			}

			$filtre=" WHERE num_bon > 0 AND ";

			if (isset($_GET['filtrer']))
			{
				// MAJ des variables de session de filtre si il y a eu demande de filtrage
				if (isset($_GET['filtre_bon_client']) && $_GET['filtre_bon_client']<>0 )
					$_SESSION['filtre_bon_client']=$_GET['filtre_bon_client'];
				if (isset($_GET['filtre_bon_client']) && $_GET['filtre_bon_client']==0 )
					unset($_SESSION['filtre_bon_client']);
				if (isset($_GET['filtre_bon_num']))
					$_SESSION['filtre_bon_num']=$_GET['filtre_bon_num'];
				if (isset($_GET['filtre_bon_date']))
					$_SESSION['filtre_bon_date']=$_GET['filtre_bon_date'];
				if (isset($_GET['filtre_bon_HT']))
					$_SESSION['filtre_bon_HT']=$_GET['filtre_bon_HT'];
				if (isset($_GET['filtre_bon_TTC']))
					$_SESSION['filtre_bon_TTC']=$_GET['filtre_bon_TTC'];
				if (isset($_GET['filtre_a_facturer']))
				{
					$_SESSION['filtre_a_facturer']=$_GET['filtre_a_facturer'];
				} else {
					$_SESSION['filtre_a_facturer']="";
				}
				if (isset($_GET['filtre_deja_facture']))
				{
					$_SESSION['filtre_deja_facture']=$_GET['filtre_deja_facture'];
				} else {
					$_SESSION['filtre_deja_facture']="";
				}
			}
			
			//if ( ! isset($_GET['annuler_filtre']) && ! isset($_GET['filtrer']) && ! isset($_SESSION['filtre_bon_date']))


			//Création de la requete. Ne se trouve pas dans le test "Si frm poster" car si le user revient le filtre doit tj etre en place
			if (isset($_SESSION['filtre_bon_client']) && $_SESSION['filtre_bon_client'] != "" )
			{
				$filtre .= "num_client="  . $_SESSION['filtre_bon_client'] . " AND ";
			}
			if (isset($_SESSION['filtre_bon_num']) && $_SESSION['filtre_bon_num'] != "")
			{
				$filtre .= "num_bon" . saisie_to_sql($_SESSION['filtre_bon_num'],"num") . " AND ";
			}
			if (isset($_SESSION['filtre_bon_date']) && $_SESSION['filtre_bon_date'] != "")
			{
				$filtre .= "date" . saisie_to_sql($_SESSION['filtre_bon_date'],"date") . " AND ";
			}
			if (isset($_SESSION['filtre_bon_HT']) &&$_SESSION['filtre_bon_HT'] != "")
			{
				$filtre .= "tot_htva" . saisie_to_sql($_SESSION['filtre_bon_HT'],"num") . " AND ";
			}
			if (isset($_SESSION['filtre_bon_TTC']) && $_SESSION['filtre_bon_TTC'] != "")
			{
				$filtre .= "tot_tva" . saisie_to_sql($_SESSION['filtre_bon_TTC'],"num") . " AND ";
			}
			if (!isset($_SESSION['filtre_deja_facture']) || $_SESSION['filtre_deja_facture'] == "")
			{
				$filtre .= "num_facture = 0 AND ";
			}
			if (!isset($_SESSION['filtre_a_facturer']) || $_SESSION['filtre_a_facturer'] == "")
			{
				$filtre .= "num_facture != 0 AND ";
			}

			$filtre = substr($filtre,0,strlen($filtre) -5); //retirer " AND " de la fin du filtre
			$sql .= " $filtre ";


			
			//echo $sql;

			// ----------------  FIN DE GESTION DES FILTRES  -----------------------------

			$tri_ordre=isset($_GET['tri_ordre'])?$_GET['tri_ordre']:"ASC"; //ordre de tri
			$tri_champ=isset($_GET['tri_champ'])?$_GET['tri_champ']:"date"; // champ de tri

         $sql .= " ORDER BY " . $tri_champ . " " . $tri_ordre;
			
			$req = mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>" . mysql_error());


			?>
			<center>
				<?php //affichage des 2 tableaux du hauts ?>
				<table class="boiteaction" width="80%">
					<tr>
						<td width="45%">
							<form name="formu" method="get" action="bon_edit.php" onsubmit="return verif_formulaire()">
							<input type="hidden" name="nouv" value="ok" />
							<?php //tableau permettant de créer un bon de commande ?>
							<center>
							<table width="90%">
								<caption><?php echo "$lang_cre_bon"; ?></caption>
								<tr>
									<td class="texte0" id="num_client"><?php echo "$lang_client";?> </td>
									<td class="texte0">
										<?php require_once("include/choix_cli.php");
										inclure_combo_client("num_client",0,"formu");
										?>
									</td>
								</tr>
								<tr>
									<td class="texte0" id="date"><?php aff_lang("Date") ?> </td>
									<td class="texte0">
										<input type="text" name="date" value="<?php echo "$jour/" . date("m") . "/$annee" ?>" />
										<a href="#" onclick="window.open('include/pop.calendrier.php?frm=formu&amp;ch=date','calendrier','width=415,height=190,left=200,top=200,scrollbars=0').focus();">
											<img src="image/petit_calendrier.gif" alt="calendrier" border="0"/>
										</a>
									</td>
								</tr>
								<tr>
									<td class="submit" colspan="2">
										<input type="button" onclick="verif_creer_bon()" name="Submit" value="<?php echo "$lang_crer_bon" ?>" />
									</td>
								</tr>
								<tr>
									<td><br/><br/><?php bouton_aide("bon") ?></td></tr>
							</table>
							</center>
							</form>
						</td>
						<td width="5%">&nbsp;</td>
						<td width="50%">
							<?php //tableau de filtre ?>
							<form action="bon_lister.php" method="get" name="frm_cde">
								<input type="hidden" name="tri_champ" value="<?php echo $tri_champ ?>" />
								<input type="hidden" name="tri_ordre" value="<?php echo $tri_ordre ?>" />
								<input type="hidden" name="filtrer"/>
								<table width="90%">
									<caption><?php aff_lang("Filtre") ?></caption>
									<tr>
										<td class="texte0" width="20%"><?php aff_lang("Bon n°") ?></td>
										<td class="texte0" width="30%">
											<input type="text" name="filtre_bon_num" value="<?php echo isset($_SESSION['filtre_bon_num'])?$_SESSION['filtre_bon_num']:"" ?>" />
										</td>
										<td class="submit" rowspan="7" width="20%">
											<br/><br/>
											<input type="submit" value="Filtrer !"/>
											<br/><br/>
											<a href="bon_lister.php?annuler_filtre=oui"><img border="0" src="image/delete.png" alt="<?php echo $lang_supprimer; ?>" /></a>
											<br/><br/>
											<?php bouton_aide("filtre") ?>
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php aff_lang("Client") ?></td>
										<td class="texte0">
											<?php inclure_combo_client("filtre_bon_client",isset($_SESSION['filtre_bon_client'])?$_SESSION['filtre_bon_client']:"","frm_cde") ?>
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php aff_lang("Date") ?></td>
										<td class="texte0">
											<input type="text" size="14" name="filtre_bon_date" value="<?php echo isset($_SESSION['filtre_bon_date'])?$_SESSION['filtre_bon_date']:"" ?>" />
											<a href="#" onclick="window.open('include/pop.calendrier.php?frm=frm_cde&amp;ch=filtre_bon_date','calendrier','width=415,height=190,left=200,top=200,scrollbars=0').focus();">
												<img src="image/petit_calendrier.gif" alt="calendrier" border="0"/>
											</a>
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php echo $lang_total_h_tva; ?></td>
										<td class="texte0">
											<input type="text" name="filtre_bon_HT" value="<?php echo isset($_SESSION['filtre_bon_HT'])?$_SESSION['filtre_bon_HT']:"" ?>" />
										</td>
									</tr>
									<tr>
										<td class="texte0"><?php echo $lang_total_ttc; ?></td>
										<td class="texte0">
											<input type="text" name="filtre_bon_TTC" value="<?php echo isset($_SESSION['filtre_bon_TTC'])?$_SESSION['filtre_bon_TTC']:"" ?>" />
										</td>
									</tr>
									<tr>
										<td class="texte0" colspan="2">
											<?php aff_lang("A facturer") ?> 
													<input type="checkbox" value="1" name="filtre_a_facturer" <?php if (isset($_SESSION['filtre_a_facturer']) && $_SESSION['filtre_a_facturer'] != "") { echo 'checked="checked"' ;} ?> /> &nbsp; 
											<?php aff_lang("Déjà facturé") ?>
													<input type="checkbox" value="1" name="filtre_deja_facture" <?php if (isset($_SESSION['filtre_deja_facture']) && $_SESSION['filtre_deja_facture'] != "") { echo 'checked="checked"' ;} ?> /> &nbsp; 
										</td>
									</tr>
								</table>
							</form>

						</td>
					</tr>
				</table>
				<br/>

				<br/>

				<?php //Afficher la liste des commandes ?>
				<table class="boiteaction">
					<tr>
						<th width="30"><a href="javascript:ecrit_ordre_et_post('frm_cde','num_bon')">N°</a></th>
						<th width="180"><a href="javascript:ecrit_ordre_et_post('frm_cde','nom')"><?php echo $lang_client; ?></a></th>
						<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_cde','date')"><?php echo $lang_date; ?></a></th>
						<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_cde','tot_htva')"><?php  aff_lang("Total HT")  ?></a></th>
						<th width="90"><a href="javascript:ecrit_ordre_et_post('frm_cde','ttc')"><?php  aff_lang("Total TTC")  ?></a></th>
						<th colspan="6"><?php echo $lang_action; ?>  &nbsp; <?php bouton_aide("bon_icones") ?></th>
					</tr>
      			<?php
					$nombre = 1;
					$total_ht=0;
					$total_ttc=0;
					while($data = mysql_fetch_array($req))
					{
						$num_bon = $data['num_bon'];
						$total = $data['tot_htva'];
						$tva = $data["tot_tva"];
						$date = sql_to_date($data["date"]);
						$nom = $data['nom'];
						$nom = htmlentities($nom, ENT_QUOTES,'UTF-8');
						$nom_html = htmlentities (urlencode ($nom));
						$num_client = $data['num_client'];
						$mail = $data['mail'];
						$login = $data['login'];
						$ttc = $data['ttc'];
						$nombre = $nombre +1;
						$num_facture = $data['num_facture'];
						if($nombre & 1) {
							$line=0;
						} else {
							$line=1;
						}
						$total_ht = $total_ht + $total;
						$total_ttc = $total_ttc + $ttc;
						
					?>
					<tr class="texte<?php echo $line ?>" onmouseover="this.className='highlight'" onmouseout="this.className='texte<?php echo "$line" ?>'">
						<td class="highlight"><?php echo "$num_bon"; ?></td>
						<td class="highlight"><?php echo "$nom"; ?></td>
						<td class="highlight"><?php echo "$date"; ?></td>
						<td class="highlight" style="text-align:right"><?php echo montant_financier($total); ?>&nbsp;</td>
						<td class="highlight" style="text-align:right"><?php echo montant_financier($ttc); ?>&nbsp;</td>
						<td class="highlight">
							<a href='bon_edit.php?num_bon=<?php echo "$num_bon"; ?>'>
								<img border="0" alt="editer" src="image/edit.png" />
							</a>
						</td>
						<?php
						if ($num_facture == "0")
						{
						?>
						<td class="highlight">
							<a href='bon_delete.php?num_bon=<?php echo $num_bon; ?>&amp;nom=<?php echo "$nom_html"; ?>' 
								onclick="return confirmDelete('<?php echo"$lang_con_effa $num_bon"; ?>')"><img border="0" src="image/delete.png" alt="delete" /></a>
						</td>
						<?php
						} else {
						?>
						<td class="highlight">&nbsp;</td>
						<?php
						}
						?>
						<td class="highlight">
							<form action="fpdf/bon_pdf.php" method="post" target="_blank" >
								<input type="hidden" name="num_bon" value="<?php echo "$num_bon"; ?>" />
								<input type="hidden" name="nom" value="<?php echo "$nom_html"; ?>" />
								<input type="hidden" name="user" value="adm" />
								<input type="image" src="image/imprimante.png" alt="imprimer" />
							</form>
						</td>
						<?php
						if ($mail != '' and $login !='') { ?>
							<td class="highlight">
								<a href='notifi_cli.php?type=comm&amp;mail=<?php echo"$mail"; ?>'>
									<img src='image/mail.gif' border='0' alt='mail' onclick="return confirmDelete('<?php echo "$lang_con_env_notif $num_bon"; ?>')" />
								</a>
							</td>
						<?php
						} else {
							?>
							<td class="highlight">&nbsp;</td>
							<?php
						}
						if ($mail != '' ) {
						?>
						<td class="highlight">
							<form action="fpdf/bon_pdf.php" method="post" onclick="return confirmDelete('<?php echo "$lang_con_env_pdf $num_bon"; ?>')">
								<input type="hidden" name="num_bon" value="<?php echo $num_bon; ?>" />
								<input type="hidden" name="nom" value="<?php echo $nom; ?>" />
								<input type="hidden" name="user" value="adm" />
								<input type="hidden" name="ext" value=".pdf" />
								<input type="hidden" name="mail" value="y" />
								<input type="image" src="image/pdf.gif" alt="mail" />
							</form>
						</td>
						<?php
						} else {
							?>
							<td class="highlight">&nbsp;</td>
							<?php
						}
						?>
						<td class="highlight">
							<?php
							if ($num_facture == "0")
							{
								//il faut ajouter le num client afin de pouvoir ajouter sur l'ecran suivant d'autre bon de ce client
								echo "<a href='fact_bon_orph.php?num=$num_bon&amp;client=$num_client'>" . get_lang("Facturer ce bon") . "</a>";
							} else {
								//proposer d'afficher la facture qui contient ce bon
								echo "<a href='fact_edit.php?num_fact=$num_facture'>" . get_lang("Voir la facture n°") . "$num_facture";
							}
							?>
						</td>
					</tr>
					<?php
					} //fin while ?>
	
					<tr>
						<td colspan="3">&nbsp;</td>
						<td colspan="2"> <hr/></td>
						<td colspan="6">&nbsp;</td>
					</tr>


					<tr>
						<td colspan="2">&nbsp;</td>
						<td style="text-align:center">TOTAL</td>
						<td style="text-align:right"><?php echo montant_financier($total_ht) ?>&nbsp; </td>
						<td style="text-align:right"><?php echo montant_financier($total_ttc) ?>&nbsp; </td>
						<td colspan="6">&nbsp;</td>
					</tr>
				</table>
			</center>
		</td>
	</tr>
	<tr>
		<td>
			<?php
			require_once("include/bas.php");
			?>
		</td>
	</tr>

</table>
</body>
</html>


